﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using System.Xml.XPath;
using System.Xml;
using System.Diagnostics;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public XmlDocument xmlDocument = new XmlDocument();
        bool stop = false;
        int bookNumber = 0;
        int chapterNumber = 0; 
        int verseNumber = 0;


        public Form1()
        {
            InitializeComponent();

            //OpenFileDialog ofd = new OpenFileDialog();
            //ofd.ShowDialog();
            //string file = ofd.FileName;

            openFileDialog1.ShowDialog();
            string file = openFileDialog1.FileName;

            
            xmlDocument.Load(new XmlTextReader(file));

            findVerse();
        }

        private void textBox_TextChanged(object sender, EventArgs e)
        {
            //findVerse();
        }

        private void buttonSearch_Click(object sender, EventArgs e)
        {
            findVerse();
        }

        private void textBox_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter) findVerse();
        }

        //private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        //{
        //    webBrowser1.Url = new Uri("http://sourceforge.net/projects/zefania-sharp/");
        //}

        public void richTextBox1Clear()
        {
            richTextBox1.Clear();
        }

        public void richTextBox2Clear()
        {
            richTextBox2.Clear();
        }

        public void richTextBox1Set(string text)
        {
            richTextBox1.Text = text;
        }

        public void richTextBox1AppendText(string text)
        {
            richTextBox1.AppendText(text);
        }

        public void richTextBox2AppendText(string text)
        {
            richTextBox2.AppendText(text);
        }

        private void findVerse()
        {
            //string xpath = "//BIBLEBOOK[@bsname='1Mo']/CHAPTER[@cnumber=1]/VERS[@vnumber=1]";

            //string xpath = String.Format(
            //    "//BIBLEBOOK[@bsname='{0}']/CHAPTER[@cnumber={1}]/VERS[@vnumber={2}]",
            //    textBoxBook.Text, textBoxChapter.Text, textBoxVers.Text);

            string xPath = String.Format(
                            "//BIBLEBOOK[@bnumber>='{0}'] and BIBLEBOOK[@bnumber<='{1}']/CHAPTER[@cnumber>={2}] and CHAPTER[@cnumber<={3}]/VERS[@vnumber>={4} and @vnumber <={5}]",
                            textBoxBook.Text, textBoxBookStop.Text, textBoxChapter.Text, textBoxChapterStop.Text, 
                            textBoxVers.Text, textBoxVerseStop.Text);

            //ZefaniaXmlParser.findVerse(XmlDoc, xPath);
            //richTextBox1.Text = ZefaniaXmlParser.bibleVerses;
            //richTextBox2.Text = ZefaniaXmlParser.footnotes;

            //richTextBox1.Text = XmlDoc.InnerText;

            richTextBox1.Text = xmlDocument.DocumentElement.Name + ":\n";

            stop = false;
            printXmlElement(xmlDocument.DocumentElement, 0);
        }

        private void printXmlElement(XmlNode xmlElement, int indent)
        {
            printLine(indent, xmlElement.Name);

            if (xmlElement.Attributes != null)
                foreach (XmlAttribute xmlAttribute in xmlElement.Attributes) {
                    printXmlAttributes(xmlElement, indent, xmlAttribute);
                if (!stop) 
                    if (xmlElement.HasChildNodes)
                        foreach (XmlNode childNode in xmlElement.ChildNodes)
                            printXmlElement(childNode, indent + 1);
                }
            else if (xmlElement.Value != null) printLine(indent, xmlElement.Value);
            else printLine(indent, xmlElement.InnerXml);
        }

        private void printXmlAttributes(XmlNode xmlElement, int indent, XmlAttribute xmlAttribute)
        {
            if (xmlAttribute.Name == "bnumber")
            {
                bookNumber = int.Parse(xmlAttribute.Value);
                if (int.Parse(xmlAttribute.Value) > 40)
                    stop = true;
            }
            if (xmlAttribute.Name == "cnumber")
                chapterNumber = int.Parse(xmlAttribute.Value);
            if (xmlAttribute.Name == "vnumber")
                verseNumber = int.Parse(xmlAttribute.Value);
            printLine(indent, xmlAttribute.Name + ":\t" + xmlAttribute.Value + '\n');
        }

        private void printLine(int indent, string str)
        {
            for (int i = 0; i < indent; i++)
            {
                richTextBox1.AppendText("\t");
                Debug.Write("\t");
            }
            richTextBox1.Text = str + ":\n";
            Debug.WriteLine(bookNumber.ToString() + ':' + chapterNumber.ToString() + ':' + 
                verseNumber.ToString() + ":\t" + str);
        }

        private void dateiToolStripMenuItem_Click(object sender, EventArgs e)
        {
            openFileDialog1.ShowDialog();
            string file = openFileDialog1.FileName;

            xmlDocument.Load(new XmlTextReader(file));

            findVerse();
        }

        private void fontToolStripMenuItem_Click(object sender, EventArgs e)
        {
            fontDialog1.ShowDialog();
            richTextBox1.Font = fontDialog1.Font;
        }

        private void farbenToolStripMenuItem_Click(object sender, EventArgs e)
        {
            colorDialog1.ShowDialog();
            richTextBox1.BackColor = colorDialog1.Color;
        }
    }       // class
}           // namespace
